/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 directory_changer.h

	$Header: /heroes4/directory_changer.h $

	$NoKeywords: $

 ************************************************************************/

#if !defined( DIRECTORY_CHANGER_H_INCLUDED )
#define DIRECTORY_CHANGER_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <cstdlib>
#include <direct.h>

#include "constructor_failure.h"

// ----------------------------------------------------------------------
// t_directory_changer class
// ----------------------------------------------------------------------

class t_directory_changer
{
public:
	// Types
	typedef t_constructor_failure< t_directory_changer > t_create_failure;

	// Constructor/Destructor
	t_directory_changer( char const * directory_name );
	t_directory_changer( char const* directory_name, bool* changed );
	~t_directory_changer();

private:
	// Data members
	char	m_old_directory_name[ _MAX_PATH ];
};

inline t_directory_changer::t_directory_changer( char const * directory_name )
{
	_getcwd( m_old_directory_name, _MAX_PATH );
	m_old_directory_name[ _MAX_PATH - 1 ] = '\0';
	if ( _chdir( directory_name ) != 0 )
		throw t_create_failure();
}

inline t_directory_changer::t_directory_changer( char const* directory_name, bool* changed )
{
	bool result;

	_getcwd( m_old_directory_name, _MAX_PATH );
	m_old_directory_name[ _MAX_PATH - 1  ] = '\0';
	result = (_chdir( directory_name ) == 0);
	if (changed != 0)
		*changed = result;
}

inline t_directory_changer::~t_directory_changer()
{
	_chdir( m_old_directory_name );
}

#endif // !defined( DIRECTORY_CHANGER_H_INCLUDED )
